﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsPaymentMethodsTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// PaymentMethods Business Object
    /// </summary>

    public class PaymentMethods : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected PaymentMethods() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// PaymentMethodID.
        /// </summary>
        public int PaymentMethodID
        {
            get { return _paymentMethodID; }
            set
            {
                if (_paymentMethodID != value)
                {
                    if (OnPropertyChanging("PaymentMethodID", _paymentMethodID, value))
                    {
                        int oldValue = _paymentMethodID;
                        _paymentMethodID = value;
                        OnPropertyChanged("PaymentMethodID", oldValue, value);
                    }
                }
            }
        }  private int _paymentMethodID = -1;

        /// <summary>
        /// UserID.
        /// </summary>
        public int UserID
        {
            get { return _userID; }
            set
            {
                if (_userID != value)
                {
                    if (OnPropertyChanging("UserID", _userID, value))
                    {
                        int oldValue = _userID;
                        _userID = value;
                        OnPropertyChanged("UserID", oldValue, value);
                    }
                }
            }
        }  private int _userID = -1;


        /// <summary>
        /// PaymentType.
        /// </summary>
        public string PaymentType
        {
            get { return _paymentType; }
            set
            {
                if (_paymentType != value)
                {
                    if (OnPropertyChanging("PaymentType", _paymentType, value))
                    {
                        string oldValue = _paymentType;
                        _paymentType = value;
                        OnPropertyChanged("PaymentType", oldValue, value);
                    }
                }
            }
        }  private string _paymentType = null;


        /// <summary>
        /// FinancialAccount.
        /// </summary>
        public string FinancialAccount
        {
            get { return _financialAccount; }
            set
            {
                if (_financialAccount != value)
                {
                    if (OnPropertyChanging("FinancialAccount", _financialAccount, value))
                    {
                        string oldValue = _financialAccount;
                        _financialAccount = value;
                        OnPropertyChanged("FinancialAccount", oldValue, value);
                    }
                }
            }
        }  private string _financialAccount = null;


        /// <summary>
        /// CardType.
        /// </summary>
        public string CardType
        {
            get { return _cardType; }
            set
            {
                if (_cardType != value)
                {
                    if (OnPropertyChanging("CardType", _cardType, value))
                    {
                        string oldValue = _cardType;
                        _cardType = value;
                        OnPropertyChanged("CardType", oldValue, value);
                    }
                }
            }
        }  private string _cardType = null;


        /// <summary>
        /// CardNumber.
        /// </summary>
        public string CardNumber
        {
            get { return _cardNumber; }
            set
            {
                if (_cardNumber != value)
                {
                    if (OnPropertyChanging("CardNumber", _cardNumber, value))
                    {
                        string oldValue = _cardNumber;
                        _cardNumber = value;
                        OnPropertyChanged("CardNumber", oldValue, value);
                    }
                }
            }
        }  private string _cardNumber = null;


        /// <summary>
        /// ExpirationMonth.
        /// </summary>
        public string ExpirationMonth
        {
            get { return _expirationMonth; }
            set
            {
                if (_expirationMonth != value)
                {
                    if (OnPropertyChanging("ExpirationMonth", _expirationMonth, value))
                    {
                        string oldValue = _expirationMonth;
                        _expirationMonth = value;
                        OnPropertyChanged("ExpirationMonth", oldValue, value);
                    }
                }
            }
        }  private string _expirationMonth = null;


        /// <summary>
        /// ExpirationYear.
        /// </summary>
        public string ExpirationYear
        {
            get { return _expirationYear; }
            set
            {
                if (_expirationYear != value)
                {
                    if (OnPropertyChanging("ExpirationYear", _expirationYear, value))
                    {
                        string oldValue = _expirationYear;
                        _expirationYear = value;
                        OnPropertyChanged("ExpirationYear", oldValue, value);
                    }
                }
            }
        }  private string _expirationYear = null;


        /// <summary>
        /// CVVNumber.
        /// </summary>
        public string CVVNumber
        {
            get { return _cVVNumber; }
            set
            {
                if (_cVVNumber != value)
                {
                    if (OnPropertyChanging("CVVNumber", _cVVNumber, value))
                    {
                        string oldValue = _cVVNumber;
                        _cVVNumber = value;
                        OnPropertyChanged("CVVNumber", oldValue, value);
                    }
                }
            }
        }  private string _cVVNumber = null;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty PaymentMethods object.
        /// </summary>
        public static PaymentMethods CreateNew()
        {
            PaymentMethods obj = new PaymentMethods();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static PaymentMethods CreateNew(PaymentMethodsCollection collection)
        {
            PaymentMethods obj = CreateNew();
            collection.Add(obj);
            return (obj);
        }

        #endregion

        #region GetPaymentMethodByID

        /// <summary>
        /// Returns an JobCategory object for the specified 'JobCategoryID' or null if no JobCategory with the specified parameter(s) exists.
        /// </summary>
        public static PaymentMethods GetPaymentMethodByID(int paymentmethodid)
        {
            PaymentMethodsTableAdapter adapter = new PaymentMethodsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsPaymentMethods.PaymentMethodsDataTable tbl = adapter.GetData(paymentmethodid);
            if (tbl.Rows.Count == 1)
            {
                PaymentMethods p = new PaymentMethods();
                RelationalToObject(p, tbl.Rows[0]);
                return (p);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'PaymentMethodID' returned more than one record.");

            return (null);
        }

        #endregion

        #region GetPaymentMethodByCardNumber

        /// <summary>
        /// Returns an PaymentMethod object for the specified 'Creditcardnumber' or null if no PaymentMethod with the specified parameter(s) exists.
        /// </summary>
        public static PaymentMethods GetPaymentMethodByCardNumber(string creditcardnumber)
        {
            PaymentMethodsTableAdapter adapter = new PaymentMethodsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsPaymentMethods.PaymentMethodsDataTable tbl = adapter.GetDataByCreditCardNumber(creditcardnumber);
            if (tbl.Rows.Count == 1)
            {
                PaymentMethods p = new PaymentMethods();
                RelationalToObject(p, tbl.Rows[0]);
                return (p);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'Creditcardnumber' returned more than one record.");

            return (null);
        }

        #endregion

        #region GetCompletePaymentMethodsByUserID

        /// <summary>
        /// Returns a collection of PaymentMethods.
        /// </summary>
        public static PaymentMethodsCollection GetCompletePortfolioByUserID(int userid)
        {
            PaymentMethodsTableAdapter adapter = new PaymentMethodsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsPaymentMethods.PaymentMethodsDataTable tbl = adapter.GetDataByUserID(userid);

            PaymentMethodsCollection list = new PaymentMethodsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsPaymentMethods.PaymentMethodsRow row in tbl.Rows)
                {
                    PaymentMethods paymentMethods = new PaymentMethods();
                    RelationalToObject(paymentMethods, row);
                    list.Add(paymentMethods);
                }
            }

            return (list);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified PaymentMethods.
        /// </summary>
        public static void Update(PaymentMethods obj)
        {
            bool updatePaymentMethods = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updatePaymentMethods = (obj.ObjState != ObjectState.Unchanged);

                if (!updatePaymentMethods)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsPaymentMethods.PaymentMethodsDataTable tbl = null;
            if (updatePaymentMethods)
            {
                PaymentMethodsTableAdapter adapter = new PaymentMethodsTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();


                tbl = new dsPaymentMethods.PaymentMethodsDataTable();
                dsPaymentMethods.PaymentMethodsRow row = tbl.NewPaymentMethodsRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Delete

        /// <summary>
        /// Delete a file.
        /// </summary>
        public static int Delete(int paymentMethodID)
        {
            PaymentMethodsTableAdapter adapter = new PaymentMethodsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            int d = adapter.Delete(paymentMethodID);

            return (d);
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("PaymentMethodID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("PaymentMethods: PaymentMethodID={0}, UserID={1}, {2}", PaymentMethodID, UserID));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more SubAdmin objects.
    /// </summary>

    public class PaymentMethodsCollection : List<PaymentMethods>
    {

        #region Types

        public class UserComparer : IComparer<PaymentMethods>
        {
            public int Compare(PaymentMethods pm1, PaymentMethods pm2)
            {
                return (String.Compare(pm1.PaymentMethodID.ToString(), pm2.PaymentMethodID.ToString()));
            }
        }

        #endregion

    }
}
